<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { $t } from '@/locales';
import { useAntdForm } from '@/hooks/common/form';
import { deviceHooks } from '@/views/breedingManage/deviceManage/deviceHooks';
import { useProjectStore } from '@/store/modules/project';
const projectStore = useProjectStore();
defineOptions({
  name: 'UserSearch'
});

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const { formRef, validate, resetFields } = useAntdForm();

const model = defineModel<any>('model', {
  required: true
});

const deviceList = ref([]);
const initDeviceList = async () => {
  const { getDeviceList } = deviceHooks();
  deviceList.value = await getDeviceList({
    projectId: projectStore.currentProject?.id,
    parentId: 0
  });
};

onMounted(() => {
  initDeviceList();
});

async function reset() {
  await resetFields();
  emit('reset');
  emit('search');
}

async function search() {
  await validate();
  emit('search');
}
</script>

<template>
  <ACard :bordered="false" class="card-wrapper">
    <AForm
      ref="formRef"
      :model="model"
      :label-col="{
        span: 5,
        md: 7
      }"
    >
      <ARow :gutter="[16, 16]" wrap>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="设备编码" name="deviceId" class="m-0">
            <ASelect
              v-model:value="model.deviceId"
              placeholder="请选择设备编码"
              clearable
              :dropdown-match-select-width="false"
            >
              <ASelectOption v-for="item in deviceList" :key="item.id">
                <div class="flex justify-between">
                  <div>{{ item.deviceNo }}</div>
                  <div style="color: #8492a6; margin-left: 30px">{{ item.assetName }}</div>
                </div>
              </ASelectOption>
            </ASelect>
          </AFormItem>
        </ACol>
        <div class="flex-1">
          <AFormItem class="m-0">
            <div class="w-full flex-y-center justify-end gap-12px">
              <AButton @click="reset">
                <template #icon>
                  <icon-ic-round-refresh class="align-sub text-icon" />
                </template>
                <span class="ml-8px">重置</span>
              </AButton>
              <AButton type="primary" ghost @click="search">
                <template #icon>
                  <icon-ic-round-search class="align-sub text-icon" />
                </template>
                <span class="ml-8px">搜索</span>
              </AButton>
            </div>
          </AFormItem>
        </div>
      </ARow>
    </AForm>
  </ACard>
</template>

<style scoped></style>
